package email

import (
	"fmt"
	"log"
	"net/smtp"
)

// variables to make ExamplePlainAuth compile, without adding
// unnecessary noise there.
var (
	from       = "lyw_programer@163.com"
	msg        = []byte("test message")
	recipients = []string{"378305181@qq.com"}
	hostname   = "smtp.163.com"
	password   = "UCANTFPSLCFNRPEK"
)

func SendEmail(filePath, fileName, body string) error {
	var mail Mail
	mail = &SendMail{User: "lyw_programer@163.com", Password: "UCANTFPSLCFNRPEK", Host: "smtp.163.com", Port: "25"}
	message := Message{From: "lyw_programer@163.com",
		To: []string{"liuyawei10@huawei.com", "378305181@qq.com", "liwentao20@huawei.com", "haotuo@huawei.com", "wangyaofeng.wang@huawei.com", "yinchuang@huawei.com",
			"liuyaning7@huawei.com", "huanghuijin@huawei.com", "lubinglun@huawei.com", "xiaoxiaoliang2@huawei.com", "lijunru9@huawei.com", "guzhihao4@huawei.com", "fangting12@huawei.com",
			"chenmudan@huawei.com", "liuguangfeng2@huawei.com", "guanmingyue@h-partners.com", "hanlong7@huawei-partners.com", "jianyang9@huawei.com", "yinwei42@h-partners.com", "yedexi1@huawei.com",
			"qiuyujia1@huawei.com", "lijincheng13@huawei.com", "liyiming13@huawei.com", "wanzixuan@huawei.com"},
		//To: []string{"liuyawei10@huawei.com", "378305181@qq.com"},
		Cc:          []string{"gongjunsong@huawei.com", "pengbiao1@huawei.com"},
		Bcc:         []string{},
		Subject:     "每日测试结果",
		Body:        body,
		ContentType: "text/html; charset=UTF-8",
		Attachment: Attachment{
			Name: filePath,
			//ContentType: "image/png",
			ContentType: "application/x-zip-compressed",
			WithFile:    true,
		},
	}
	err := mail.Send(message, fileName)
	if err != nil {
		return err
	}
	return nil
}

func Example() {
	// Connect to the remote SMTP server.
	c, err := smtp.Dial("mail.example.com:25")
	if err != nil {
		log.Fatal(err)
	}

	// Set the sender and recipient first
	if err := c.Mail("sender@example.org"); err != nil {
		log.Fatal(err)
	}
	if err := c.Rcpt("recipient@example.net"); err != nil {
		log.Fatal(err)
	}

	// Send the email body.
	wc, err := c.Data()
	if err != nil {
		log.Fatal(err)
	}
	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		log.Fatal(err)
	}
	err = wc.Close()
	if err != nil {
		log.Fatal(err)
	}

	// Send the QUIT command and close the connection.
	err = c.Quit()
	if err != nil {
		log.Fatal(err)
	}
}

func ExamplePlainAuth() {
	// hostname is used by PlainAuth to validate the TLS certificate.

	auth := smtp.PlainAuth("", "lyw_programer@163.com", "UCANTFPSLCFNRPEK", hostname)

	err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)
	if err != nil {
		log.Fatal("send Email error:", err)
	}
}

func ExampleSendMail() {
	// Set up authentication information.
	auth := smtp.PlainAuth("", "lyw_programer@163.com", "UCANTFPSLCFNRPEK", hostname)

	// Connect to the server, authenticate, set the sender and recipient,
	// and send the email all in one step.
	msg := []byte("To: recipient@example.net\r\n" +
		"Subject: discount Gophers!\r\n" +
		"\r\n" +
		"This is the email body.\r\n" + "test test test\r\n")
	err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)
	if err != nil {
		log.Fatal(err)
	}
}
